package cn.bigfire.wzdj.util.tool;

import cn.bigfire.wzdj.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.bigfire.wzdj.pojo.bo.WZDJPack;
import cn.bigfire.wzdj.util.PackUtil;
import cn.bigfire.wzdj.util.ReqHexGen;

import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

public class ProtocolHandler {


    static List<Integer> wzdhIgnoreIds = new ArrayList<>();
    static {
        wzdhIgnoreIds.add(1835014);//1835014,RpcClientChatSend,rpc 客户端聊天发送
//        ignoreIds.add(65536042);
//        ignoreIds.add(65536022);
    }

    public static void onMessage(WSFrame wsFrame,int id,boolean isRequest, StringBuilder resultSb) {
        byte[] payloadBytes = wsFrame.getPayloadBytes();
//        byte[] bytes = ByteUtil.subBefore(payloadBytes, 76);
        String hex = HexUtil.encodeHexStr(payloadBytes);
        String projectName = "[未知]";
        if (hex.startsWith("32450000")) {//快来当领主
            projectName = "[快来当领主]";
            String[] packs = hex.split("3245");
            for (String pack : packs) {
                if (StrUtil.isBlank(pack)) {
                    continue;
                }
                byte[] packBytes = HexUtil.decodeHex(pack);
                byte[] lenBytes = ByteUtil.subBefore(packBytes, 4);
                byte[] crcBytes = ByteUtil.subBetween(packBytes, 4, 8);
                byte[] seqBytes = ByteUtil.subBetween(packBytes, 8, 10);
                byte[] data = ByteUtil.subAfter(packBytes, 10);
                int len1 = cn.hutool.core.util.ByteUtil.bytesToInt(lenBytes, ByteOrder.BIG_ENDIAN);
                int len2 = cn.hutool.core.util.ByteUtil.bytesToInt(lenBytes, ByteOrder.LITTLE_ENDIAN);
                String lenStr = HexUtil.encodeHexStr(lenBytes);
                String crcHex = HexUtil.encodeHexStr(crcBytes);
                short seq1 = cn.hutool.core.util.ByteUtil.bytesToShort(seqBytes, ByteOrder.BIG_ENDIAN);
                short seq2 = cn.hutool.core.util.ByteUtil.bytesToShort(seqBytes, ByteOrder.LITTLE_ENDIAN);
                String seqStr = HexUtil.encodeHexStr(seqBytes);
                String dataStr = HexUtil.encodeHexStr(data);
                String direction = isRequest ? "↑" : "↓";
                System.out.printf("%s%s[领主](%s)3245|%s|%s|%s|%s%n", direction, id, len1, lenStr, crcHex, seqStr, dataStr);
            }
        } else if (hex.startsWith("32460000")) {
            projectName = "[快来当领主]";
            String[] packs = hex.split("3246");
            for (String pack : packs) {
                if (StrUtil.isBlank(pack)) {
                    continue;
                }
                byte[] packBytes = HexUtil.decodeHex(pack);
                byte[] lenBytes = ByteUtil.subBefore(packBytes, 4);
                byte[] crcBytes = ByteUtil.subBetween(packBytes, 4, 8);
                byte[] seqBytes = ByteUtil.subBetween(packBytes, 8, 10);
                byte[] data = ByteUtil.subAfter(packBytes, 10);
                int len1 = cn.hutool.core.util.ByteUtil.bytesToInt(lenBytes, ByteOrder.BIG_ENDIAN);
                int len2 = cn.hutool.core.util.ByteUtil.bytesToInt(lenBytes, ByteOrder.LITTLE_ENDIAN);
                String lenStr = HexUtil.encodeHexStr(lenBytes);
                String crcHex = HexUtil.encodeHexStr(crcBytes);
                short seq1 = cn.hutool.core.util.ByteUtil.bytesToShort(seqBytes, ByteOrder.BIG_ENDIAN);
                short seq2 = cn.hutool.core.util.ByteUtil.bytesToShort(seqBytes, ByteOrder.LITTLE_ENDIAN);
                String seqStr = HexUtil.encodeHexStr(seqBytes);
                String dataStr = HexUtil.encodeHexStr(data);
                String direction = isRequest ? "↑" : "↓";
                System.out.printf("%s%s[领主](%s)3246|%s|%s|%s|%s%n", direction, id, len1, lenStr, crcHex, seqStr, dataStr);
            }
        } else if (hex.startsWith("42")) {
            projectName = "[快来当领主]";
            byte[] bytes = ByteUtil.subAfter(payloadBytes, 1);
//            DataBuffer dataBuffer = DataBuffer.of(bytes);
//            log.info(JSON.toJSONString(dataBuffer.toMap()));

        } else if (hex.startsWith("0100")) {//王者对决
            projectName = "[王者对决]";
            if (wsFrame.isRequest()) {//发送数据代码生成
                String code = ReqHexGen.genCode(wsFrame.getPayloadBytes());
                System.out.println(code);
                resultSb.append(code).append("\n");
            } else {//响应数据解析
                List<WZDJPack<?>> packList = PackUtil.decodeBytes(wsFrame.getPayloadBytes());
                for (WZDJPack<?> wzdjPack : packList) {
                    //跳过一些 频率高，但是没啥用的数据包
                    if (wzdhIgnoreIds.contains(wzdjPack.getHeader().getId())) {
                        System.out.println("跳过 数据包 id:" + wzdjPack.getHeader().getId());
                        continue;
                    }
                    String code = wzdjPack.toString();
                    System.out.println(code);
                    resultSb.append(code).append("\n");
                }
            }
        } else if (hex.startsWith("xxxxx")) {//待扩展类型

        }

    }

}
